package com.yuncheng.spcyApi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.service.IYcSysOrgService;
import com.yuncheng.spcyApi.service.IYcSysUserService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "审评查验-系统用户接口")
//@RestController
@RequestMapping("/api/spzx-spcy/sysUser")
public class SpcyUserController {
    @Resource
    private IYcSysUserService ycSysUserService;

    @Resource
    private IYcSysOrgService ycSysOrgService;

    @Log(title = "获取审评中心部门信息")
    @ApiOperation(value = "获取审评中心部门信息")
    @GetMapping(value = "querySpzxDeptList")
    public HttpResult<?> querySpzxDeptList(){
        // 审评中心 - 主部门下的 子部门
        List<YcSysOrg> orgList = ycSysOrgService.list(
                new LambdaQueryWrapper<YcSysOrg>()
                .eq(YcSysOrg::getParentId, "1632951314992041985")
                .orderByAsc(YcSysOrg::getOrgOrder));

        return HttpResult.ok(orgList);
    }

    @Log(title = "分页获取系统用户信息")
    @ApiOperation(value = "分页获取系统用户信息")
    @GetMapping(value = "queryUserPageList")
    public HttpResult<?> queryUserPageList(YcSysUser ycSysUser,
                                           @RequestParam(value = "type",defaultValue = "")String type,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<YcSysUser> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<YcSysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(ycSysUser.getRealname()),YcSysUser::getRealname,ycSysUser.getRealname());
        queryWrapper.like(StringUtils.isNotBlank(ycSysUser.getFgzzt()),YcSysUser::getFgzzt,ycSysUser.getFgzzt());
        queryWrapper.like(StringUtils.isNotBlank(ycSysUser.getFssqy()),YcSysUser::getFssqy,ycSysUser.getFssqy());
        queryWrapper.like(StringUtils.isNotBlank(ycSysUser.getOrgId()),YcSysUser::getOrgId,ycSysUser.getOrgId());
        queryWrapper.like(StringUtils.isNotBlank(ycSysUser.getOrgName()),YcSysUser::getOrgName,ycSysUser.getOrgName());
        queryWrapper.like(StringUtils.isNotBlank(type),YcSysUser::getOrgName,type);
        ycSysUserService.page(page,queryWrapper);

        return HttpResult.ok();
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @Log(title = "获取当前登录用户信息")
    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping(value = "获取当前登录用户信息")
    public HttpResult<?> queryNowUser(){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("登录已过期，请重新登录");
        }
        YcSysUser user = ycSysUserService.getById(currentUser.getId());
        return HttpResult.ok(user);
    }


}
